!function(window,document,$,undefined) {
	var isHide = true,//标识购物篮显示隐藏
		longitude,//全局经度
		latitude,//全局纬度;
		laundryId = getUrlParam(laundryId),
		tabIndex=0,//默认显示'服务'
		laundryId = 128;//上线前删除
	var basketJson = JSON.parse(localStorage.getItem('basketJson'+laundryId))||[];
	$(document).ready(function(){

		
		var getUrl = getWashUrl();
		// var getUrl = 'http://192.168.1.249:8088/saywash/WashCallApi';
		var initList = function() {
			getLocation();//根据浏览器获取位置信息并给全局变量longitude和latitude赋值
			laundryDetail(); //店铺详情
			tab();
			bindEvt();
			getBusinessHours();
			getWashClassifical();
			laundryEvaluation(laundryId,1);
			// getWashClassificalByClassId();//根据需要传参数做渲染
		};
		var bindEvt = function() {
			$('.cart').on('tap',onCartTap);
			$('.basket_close').on('tap',onCartTap);
			$(window).on('scroll',onWinScroll);
			$('.pic').off('tap').on('tap',onLdyPicTap);//点击店铺小图查看大图
			$('.laundry_header').on('tap','.collect', onCollectTap);
			$('.store_time').on('tap',onStoreTimeTap);
			$('#third_tab_body').on('tap','.plus',onPlusTap);
			$('#shopping_backet').on('tap','.basket_check',onBaskertItemMarkTap);
		};

		var onBaskertItemMarkTap = function() { //衣物是否选中
			var $this = $(this);
			$this.toggleClass('cancle');
			console.log($this)
		}

		var onStoreTimeTap = function() { //点击营业时间跳转至时间选择界面（待完善）TODO
			window.location.href = 'selecttime.html';
		};

		var onPlusTap = function() { //点击加号时商品信息加入购物车(从本地取出对应的json字符串)
			var ThisId= this.id;
			var jsonWashJson = JSON.parse(localStorage.getItem('washJson'));
			var tmpObj = jsonWashJson[ThisId];
			var tmpArr=[];
			var everyObj = {};
			var n;
			$.each(basketJson,function(i,obj) { //取到tapCount
				if(ThisId==i) {
					n = obj.tapCount||0;
				}
			});
			if(!n) {n=0};
			everyObj[ThisId]=tmpObj;

			// console.log(everyObj);
			console.log(jsonWashJson);
			basketJson.push({ThisId:tmpObj});
			console.log(basketJson);

			// $.each(uniqArr,function(index,item) {
			// 	console.log(index)
			// })
			tmpArr.push(
				'<div class="basket_item">',
					'<div class="basket_check">',
						'<img src="../images/checked-mark.png" alt="">',
					'</div>',
					'<div class="basket_img">',
						'<img src=',tmpObj.image,' alt="">',
					'</div>',
					'<div class="basket_item_name">',tmpObj.name,'</div>',
					'<div class="basket_item_price"><span>&yen</span><span>',tmpObj.salePrice,'</span></div>',
					'<div class="addition">',
						'<div class="minus"><div class="minus_icon"></div></div>',
						'<div class="count"></div>',
						'<div class="plus">',
							'<div class="plus_h"></div>',
							'<div class="plus_v"></div>',
						'</div>',
					'</div>',
				'</div>'
			);
			$('.basket_item_wp').append(tmpArr.join(''));


		};

		var onLdyPicTap = function() {//点击店铺小图查看大图TODO
			var mySwiper; 
			$('.large_img').show();
			$('.body_masker').css({'display':'block','z-index':2});
		    mySwiper = new Swiper('.swipe-container',{paginationClickable: false});
		    // $('.large_img').on('tap','img',function(){$('.img').off('tap')})
			$('.large_img').on('tap',function() {$('.large_img').hide();$('.body_masker').css({'display':'none','z-index':8});});
		};

		var onCollectTap = function() { //收藏时需要用户登录TODO
			var $this = $(this),
				url = '/api/laundry/manageFavoriteLaundry.api';
			if($this.attr('data-isFavorite')=='F') {
				$.ajax({})
			}
		};

		function laundryDetail() { //店铺详情
			var url = getUrl+'/common/laundry/getLaundryDetailInfomation.api',
				// signData = {laundryId:128/,longitude:116.40387397,latitude:39.91488908},
				data = {
					laundryId:128,
					longitude:116.40387397,
					latitude:39.91488908,
					ssid:getTime(),
					sign:getObjectString({laundryId:128,longitude:116.40387397,latitude:39.91488908})
				};
			$.ajax({
				method:'GET',
				url:url,
				data:data,
				dataType:'json',
				success:onLaundryDetailSuccess
			})
		}

		var onLaundryDetailSuccess = function(response) { //店铺详情接口数据渲染**************************************
			console.log(response);
			var data = response.data,
				pics = data.picture,
				tmpArr = [],
				svFtArr = [],
				serviceFeatures = data.serviceFeatures;
			// data.isFavorite='T';
			// data.laundryactivityinfos='';
			// ata.businessStatus==2;
			data.star = 3;//测试修改，上线前必须删除
			lightStar(data.star);//点亮星星个数
			$('.laundry_name').html(data.name);//店铺名字
			$('#store_name').html(data.name);//详情项
			$('#deliveryPrice').html(parseInt(data.deliveryPrice).toFixed(2));//运费
			$('#startPrice').html(parseInt(data.startPrice).toFixed(2));//起送费
			$('#ld_distance').html(data.distance)//修改接口后将增加distance字段
			if(data.isFavorite=='F') {//未收藏
				$('.collect img').attr('src','../images/collect.png');
				$('.collect img').attr('data-isFavorite','F');
			};
			if(data.isFavorite=='T') {//已经收藏
				$('.collect img').attr('src','../images/had_collect.png');
				$('.collect img').attr('data-isFavorite','T');
			};
			if(data.laundryactivityinfos) {$('.youhui').show()};//优惠活动存在
			if(!data.laundryactivityinfos) {$('.youhui').hide()};//优惠活动不存在（需要修改star和fee_ds的样式）
			if(data.businessStatus==1||!data.businessStatus) {$('.masker_wrap').css('display','block')};//打烊时出现蒙版(打烊后图片能点？？？)
			if (data.businessStatus==2) {$('.masker_wrap').css('display','none')};//店铺营业状态下去掉蒙版
			if(!data.picture) {$('#ldypiconly').src = '../images/ldy_default.png';$('.pic').off('tap')}//店铺没有上传图片时显示默认图片并取消点击事件
			if(data.picture.length>0) { //店铺上传图片，可点击显示大图(没有默认显示第几张图，现取第一张)
				$('#ldypiconly').attr('src',data.picture[0]);
				$.each(pics,function(i,obj) {
					tmpArr.push(
						'<div class="swiper-slide"><img class="img" src="'+obj+'" alt=""></div>'
					)
				});
				$('#laundry_imgwp').html(tmpArr.join(''));
			}
			// $('.store_name div:nth-child(2)').html(data.name)
			$('#store_address').html(data.address);
			$('#store_tel').html(data.hotLine);
			$('#store_total').html(data.sumOrder);
			$('#scope_count').html(data.deliveryScope);
			$.each(serviceFeatures,function(index,item) {
				svFtArr.push(
					'<div class="one_descript">',
						'<div></div>',
						'<div>',item,'</div>',
					'</div>'
				);	
			});
			$('.serve_descript').html(svFtArr.join(''));
		};

		var onGetWashClassificalSuccess = function(response) { //一级分类二级分类及页面初始化显示
			var data = response.data,
				tmpArr = [],
				lis,
				// firstId = data[0].firstClass,
				paddingTop = $('#third_tab_header').css('padding-top'),
				paddingBottom = $('#third_tab_header').css('padding-bottom');
			console.log(data);
			$.each(data,function(i,obj) {
				tmpArr.push(
					'<li data-id="',obj.firstClass,'" id="ft',obj.firstClass,'">',
						'<div class="pic_wp">',
							'<div class="clothesimg_wp">',
								'<img src='+obj.firstClassImage+'>',
							'</div>',
						'</div>',
						'<div class="name_word">'+obj.firstClassName+'</div>',
					'</li>'
					);	
			});
			$('#second_tab_ul').html(tmpArr.join(''));//一级分类展示完毕(必展示)
			getWashClassificalByClassId(laundryId,data[0].firstClass,1);//展示一级分类下面的第一个项目的内容
			$lis = $('#second_tab_ul>li');
			$lis.on('tap',function(indexLi) {
				var sdArr = [],
					$this=  $(this),
					idIndex,
					len,
					index; 
				if(indexLi._args||indexLi._args==0){
					$this=$lis.eq(indexLi._args)
				}
				index = $this.index();
				$this.find('.pic_wp').addClass('on').end().siblings().find('.pic_wp').removeClass('on');
				$this.find('.name_word').addClass('on').end().siblings().find('.name_word').removeClass('on');
				$('.swiper-container').attr('id','containerId'+index);
				idIndex = $('.swiper-container').attr('id');	
				// console.log(paddingTop);
				$.each(data,function(i,obj) { //如果有二级分类则展示二级分类，没有不展示（我勒个去！）
					var swiper;
					// console.log(obj.datasecond);
					if(index==i) {//拿到与当前点击分类对应的二级分类数据
						if(obj.datasecond) {//有二级分类
							len = obj.datasecond.length;
							$.each(obj.datasecond,function(i,obj) {
								sdArr.push(
									'<div data-secondclass=',obj.secondClass,' class="swiper-slide">',obj.secondName,'</div>'
									);
							});
							// $('#third_tab_swiper').css('display','block');
							$('#third_tab_swiper').html(sdArr.join(''));
							$('#third_tab_swiper').attr('sdclassify','sdclassify');
							$('#third_tab_header').css({'padding-top':paddingTop,'padding-bottom':paddingBottom});
							swiper = new Swiper('#'+idIndex+'',{
								pagination: false,
								slidesPerView:len
							});
						}else {
							// console.log('没有二级分类');
							$('#third_tab_swiper').attr('sdclassify','');
							$('#third_tab_swiper').html('');
							$('#third_tab_header').css('padding',0);
						}
					}
				});
			});
			$lis.trigger('tap',0);//进入页面默认选中一级分类并展示其内容
		};

		var tab = function() {
			var $top_tab_header = $('#top_tab_header'),
				$header_divs = $top_tab_header.find('div'),
				$body_divs = $('#top_tab_body>div');

			$header_divs.on('tap',function() {
				var $this = $(this),
					index = $this.index();
				tabIndex = index;
				// index == 0 && $('#footer').css({'display':'-webkit-box'});	
				(index==1||index==2) && $('#footer').css('display','none');
				$this.addClass('on').siblings('.on').removeClass('on');
				$body_divs.hide().eq(index).css('display','block');
				if(index==0){$body_divs.hide().eq(index).css({'display':'-webkit-box'});$('#footer').css('display','-webkit-box');}
			})	
		};

		var onCartTap = function() {
			var $basket = $('#shopping_backet');
			isHide && $basket.animate({'bottom':'44.5px'},500,function() {isHide=false;$('.body_masker,.wash_cycle').css('display','block')});
			(!isHide) && $basket.animate({'bottom':'-262px'},500,function() {isHide=true;$('.body_masker,.wash_cycle').css('display','none')});

		};

		var onWinScroll = function() { //滚动条滚动一定距离后一级及其他选项卡置顶
			if(tabIndex!=0) {return};
			var scrollTop = $(window).scrollTop(),
				height = $('.laundry_header').height(),
				topTabH = $('.top_tab_header').height(),
				sdH = $('#third_tab_header').height(),
				// mtH = $('.gbk_nm').css('margin-bottom'),
				headH = $('.gbk_nm').height(),
				wash_item_wpMtH = parseFloat($('.wash_item_wp').css('margin-top')),
				topH = headH*1+topTabH*1,
				sd_tab_bodyWidth = $('#sd_tab_body').width(),
				H = wash_item_wpMtH+sdH;
			// console.log(mtH);
			if(scrollTop>=height) { //
				// console.log(22)
				$('#top_tab_header').addClass('fixed');//一级导航吸顶
				$('#top_tab_body').addClass('mt38');//随一级置顶设置marginTop
				$('#third_tab_header').addClass('third_header_fixed').css('top',topH+'px');//三级导航置顶
				$('#third_tab_body').css('margin-top',H+'px');//外边距为.wash_item_wpMtH的marginTop与#third_tab_header的高度和
				$('.sd_tab_header').addClass('third_header_fixed').css({'top':headH+topTabH+'px'});//左侧二级导航固定
				$('#second_tab').css('display','block');//取消display:box
				$('#sd_tab_body').css('float','right');

			}else if(scrollTop<height) {
				$('#top_tab_header').removeClass('fixed');
				$('#top_tab_body').removeClass('mt38');
				$('#third_tab_header').removeClass('third_header_fixed').css('top','')//三级导航置顶取消
				$('#third_tab_body').css('margin-top','');//取消#third_tab_body的marginTop
				$('#second_tab').css('display','box');
				$('#second_tab').css('display','-webkit-box');
				$('.sd_tab_header').removeClass('third_header_fixed').css('top','');
				$('#sd_tab_body').css({'float':'none','width':sd_tab_bodyWidth+'px'});
			}
		};

		var onLaundryEvalSuccess = function(response) {
			console.log(response);
			var data = response.data,
				wpObj = {},
				tmpArr = [];
			$.each(data,function(i,obj) {
				wpObj['o'+obj.id+'']=obj.starScore;
				tmpArr.push(
					'<div class="one_comment">',
						'<div class="comment_pic"><img src=',obj.image,' alt=""></div>',
						'<div class="comment_mid">',
							'<div class="comment_tel">',obj.phone,'</div>',
							'<div class="comment_star">',
								'<div class="wrapper" id=o',obj.id,'> ',
									 '<img src="../images/hl_star.png" alt="">',
									 '<img src="../images/hl_star.png" alt="">',
									 '<img src="../images/hl_star.png" alt="">',
									 '<img src="../images/hl_star.png" alt="">',
									 '<img src="../images/hl_star.png" alt="">',
								'</div>',
							'</div>',
							'<div class="comment_content">',obj.content,'</div>',
						'</div>',
						'<div class="comment_time">',obj.createdDate,'</div>',
					'</div>'
				);
			});
			$('.comments').append(tmpArr.join(''));
			$.each(wpObj,function(i,item) {
				// console.log(i,item)
				lightStar2(i,item)
			})

		};

		function getBusinessHours() { //  洗衣点详情页面，查询上门取衣时间接口
			var url = getUrl+'/common/laundry/getBusinessHours.api?',
			    data = {laundryId:141,ssid:getTime(),sign:getObjectString({laundryId:141})};
			$.ajax({
				method:'GET',
				url:url,
				data:data,
				dataType:'json',
				success:function(data) {
					// console.log(data);
				}
			})
		}
		function getWashClassifical() { //衣物一级分类，和二级分接口
			var url = getUrl+'/common/laundry/getWashClassifical.api',
				data = {laundryId:laundryId,ssid:getTime,sign:getObjectString({laundryId:laundryId})};
			$.ajax({
				method:'GET',
				url:url,
				data:data,
				dataType:'json',
				success:onGetWashClassificalSuccess
		    });
		}
		function laundryEvaluation(laundryId,n) {//获取洗衣店评价列表接口,传商家id和分页数n
			var url = getUrl+'/common/evaluation/getLaundryEvaluationList.api',
				data = {
					laundryId:laundryId,//
					pageNumber:n,//
					ssid:getTime,
					sign:getObjectString({laundryId:laundryId,pageNumber:n})};
				$.ajax({
					method:'GET',
					url:url,
					data,data,
					dataType:'json',
					success:onLaundryEvalSuccess
				})
		}

		function getWashClassificalByClassId(laundryId,firstId,n,secondId) { //传入商家和分类id得到对应的数据
			var url =getUrl+'/common/laundry/getWashClassificalByClassId.api',
				dataSign =  {
					laundryId:laundryId,
					firstClass:firstId,
					pageNumber:n,
				},
				sign,
				thirdArr=[];
			if(secondId) {dataSign.secondClass=secondId};
			sign = getObjectString(dataSign);
			$.extend(dataSign,{ssid:getTime(),sign:sign});
			$.ajax({
				method:"GET",
				url:url,
				data:dataSign,
				dataType:'json',
				success:function(response) {
					console.log(response);
					var data = response.data;
					var washJson = {};
					var strWashJson = '';
					
					$.each(data,function(i,obj) {
						washJson['o'+obj.thirdId] = obj;//任意地方调用该方法都会把得到的信息存在本地
						thirdArr.push(
							'<div class="wash_item_wp">',
								'<div class="wash_item">',
									'<div class="item_pic"><img src=',obj.image,' alt=""></div>',
									'<div class="description">',
										'<div class="item_name">',obj.name,'</div>',
										'<div class="cycle">洗护周期<span>',obj.washdays,'</span>天</div>',
										'<div class="item_price">',
											'<span class="now_price">&yen<span>',obj.salePrice,'</span></span>',
											'<span class="pre_price">&yen<span>',obj.oldPrice,'</span></span>',
										'</div>',
									'</div>',
								'</div>',
								'<div class="time_discount">',
									'<div>',obj.saleEndDate,'</div><span>结束</span>',
									'<div>限时折扣</div>',
								'</div>',
								'<div class="addition">',
									'<div class="minus"><div class="minus_icon"></div></div>',
									'<div class="count"></div>',
									'<div class="plus" id="o',obj.thirdId,'">',
										'<div class="plus_h"></div>',
										'<div class="plus_v"></div>',
									'</div>',
								'</div>',
							'</div>'
						);					 
					});//循环结束
					$('#third_tab_body').append(thirdArr.join(''));
					// console.log(washJson);
					strWashJson = JSON.stringify(washJson);
					localStorage.setItem('washJson',strWashJson);
					
					
				}
			});
		}	

		function getLocation() { ///获取当前经纬度
			if (!navigator.geolocation) {alert('您的浏览器不支持地理定位');return;} //用户浏览器不支持地理定位
			navigator.geolocation.getCurrentPosition(getPosition,handle_error);
			function getPosition(position) {
				var point = {};
				longitude = position.coords.longitude;
				latitude = position.coords.latitude;
				// point = JSON.stringify({longitude:longitude,latitude:latitude});
				localStorage.setItem('userLng',longitude);
				localStorage.setItem('userLat',latitude);
			}
			function handle_error(error) {
				switch(error.code){
				case 1:
					// alert("位置服务被拒绝");
					break;
				case 2:
					alert("获取不到位置信息");
					break;
				case 3:
					alert("获取信息超时");
					break;
				default:
					alert("位置错误");
					break;
				}
			}
		}

		function lightStar(n) { //点亮星星的个数方法(传商家亮星个数)
			var wrapper = $('#wrapper'),
				stars = wrapper.find('img')
				i,j,len = stars.length-n;
			for(i=0;i<n;i++) {
				stars[i].src = '../images/hl_star.png';
			};
			if(n<stars.length) {
				for(j=n;j<stars.length;j++) {
					stars[j].src = '../images/h_star.png'
				};
			};
		}

		function lightStar2(id,n) { //点亮星星的个数方法(传商家id和亮星个数)
			var wrapper = $('#'+id+''),
				stars = wrapper.find('img')
				i,j,len = stars.length-n;
			for(i=0;i<n;i++) {
				stars[i].src = '../images/hl_star.png';
			};
			if(n<stars.length) {
				for(j=n;j<stars.length;j++) {
					stars[j].src = '../images/h_star.png'
				};
			};
		}

		// var swiper = new Swiper('.swiper-container',{})
			
		
		// var swiper = new Swiper('.secondclass-swiper',{
		// 	pagination: false,
		// 	slidesPerView: 1,
		// 	// paginationClickable: true,
		// 	// spaceBetween:4.5,
		// 	freeMode: true
		// });
		initList();
	})
	
	
}(window,document,Zepto)





